import Cookies from 'js-cookie'
import router from '../../router'
export default {
  namespaced: true,
  state: {
    token: Cookies.get('token') || '',
    userInfo: {}
  },                      
  getters: {},
  mutations: {
    SET_TOKEN (state, data) {
      state.token = data
    },
    CLEAR_INFO (state) {
      state.userInfo = {}
      state.token = ''
    },
    SET_USER_INFO (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    setTokenAndUserInfo ({ commit }, { token, auto, userInfo }) {
      let arr = ['token', token]
      if (auto) arr.push({ expires: 21 })
      Cookies.set(...arr)
      commit('SET_TOKEN', token)
      commit('SET_USER_INFO', userInfo)
    },
    clearInfo ({ commit }) {
      Cookies.remove('token')
      commit('CLEAR_INFO')
      router.replace({ path: '/login' })
    },
    setUserInfo ({ commit }, userInfo) {
      commit('SET_USER_INFO', userInfo)
    }
  }
}